﻿using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI.Utils;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class NpcFollowPath : AIBaseNode
    {
        protected AIData aiData;

        protected PathDirection _pathDirection;
        
        protected override void OnExecute()
        {
            aiData = _entity.data;
            if ((aiData.npcStyle & NpcStyle.PathWalker) <= 0)
            {
                EndAction(false);
                return;
            }
            
            _entity.seeker.SwitchSpeedStrategy(SpeedStrategyType.Const);
            Vector3 pos = _transform.position;
            Vector3 forward = _transform.forward;
            //var point = LibertyAIUtils.TrafficManager.PathManager.GetNearestNavPoint(pos, forward, out PathDirection pathDirection);
            float offsetFactor = _entity.data.pathOffsetFactor;
            PathPointData point = null;
                //LibertyAIUtils.TrafficManager.PathManager.GetPathPoint(_entity.data.pathId, _entity.data.pathPointIndex);

            //if (Vector3.Angle(forward,point.position - pos) > 90)
            //{
            PathPointData closetPathPoint = LibertyAIUtils.TrafficManager.PathManager.GetClosestPathPoints(_entity.data.pathId, pos);
            if (closetPathPoint == null)
            {
                closetPathPoint = LibertyAIUtils.TrafficManager.PathManager.GetNearestPointByArea(pos, PathType.Npc);
                _entity.data.pathId = closetPathPoint.pathId;
            }
                PathPointData nextPoint = LibertyAIUtils.TrafficManager.PathManager.GetNextPoint(_entity.data.pathId, closetPathPoint.pointIndex,
                    _entity.data.pathDirection);

                Vector3 dirV = nextPoint.position - closetPathPoint.position;
                Vector3 v1 = closetPathPoint.position - pos;
                //Vector3 v2 = nextPoint.position - pos;
                if (Vector3.Angle(dirV,v1) > 90)
                {
                    point = nextPoint;
                }
                else
                {
                    point = closetPathPoint;
                }

                // point = LibertyAIUtils.TrafficManager.PathManager.GetReturnPathPoint( _entity.data.pathId,_entity.data.pathPointIndex,_entity.data.pathDirection,pos);
            //}
            
            if (point!=null)
            {
                Vector3 destination = AIGraphUtils.GetNpcDestinationFormPoint(point, GetPosition(), offsetFactor);
                
                _entity.data.destination = destination;
                _entity.data.pathId = point.pathId;
                _entity.data.pathPointIndex = point.pointIndex;
                SetDestination(destination);
            }
            
            // base.SetMaxSpeed(aiData.maxSpeed);
            // base.SetPosition(aiData.position);
            // base.SetRotation(aiData.rotation);
            // base.SetDestination(aiData.position);
        }
        
        protected override void OnUpdate()
        {
            base.SetTransformData();
            float dis = Vector3.Distance(_entity.data.destination, GetPosition());
            if (_pathDirection != _entity.data.pathDirection ||
                _entity.seeker.ReachedEndOfPath() || dis < 1.6f) // todo 应该获取到玩家RVO 和自身 RVO半径
            {
                _pathDirection = _entity.data.pathDirection;
                
                PathPointData point =
                    AIGraphUtils.GetNextPoint(aiData.pathId, aiData.pathPointIndex, aiData.pathDirection);
                float offsetFactor = _entity.data.pathOffsetFactor;
                
                if (point!=null)
                {
                    Vector3 destination = AIGraphUtils.GetNpcDestinationFormPoint(point, GetPosition(), offsetFactor);
                    _entity.data.destination = destination;
                    _entity.data.pathId = point.pathId;
                    _entity.data.pathPointIndex = point.pointIndex;
                    base.SetDestination(destination);
                }
                else
                {
                    EndAction(false);
                }

                
                // // 是否为红绿灯点位，需要判断是否进入等待
                // if (pointData.pointType == PathPointType.TrafficLight)
                // {
                //     
                // }
                // // 是否切换到了路径，可能需要转换路径行走方向
                //

            }
            
            
        }
    }
}